package com.lzxuni.controller;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lzxuni.po.*;
import com.lzxuni.service.SheBeiService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.lzxuni.service.ForwardService;
import com.lzxuni.service.YongHuService;
import com.lzxuni.utils.JsonResultUtil;
import com.lzxuni.utils.MethodUtil;
import com.lzxuni.utils.UuidUtil;

@Controller
public class ForwardController {
	@Autowired
	private ForwardService forwardService;
	@Autowired
	private YongHuService yongHuService;
	@Autowired
	private SheBeiService sheBeiService;

	@RequestMapping("/forward/list.html")
	public ModelAndView list(){
		ModelAndView mv = new ModelAndView("/forward/list");
		return mv;
	}
	
	@RequestMapping("/forward/listDo.html")
	public void listDo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int pageNumber = request.getParameter("pageNumber") == null ? 1 : Integer.parseInt(request.getParameter("pageNumber"));
		int pageSize = request.getParameter("pageSize") == null ? 10 : Integer.parseInt(request
				.getParameter("pageSize"));
		ForwardCostom forwardCostom = new ForwardCostom();
		forwardCostom.setId(UuidUtil.get32UUID());
		// 分页
		@SuppressWarnings("rawtypes")
		PageInfo pageInfo = forwardService.findForwardList(pageNumber, pageSize,forwardCostom);
		@SuppressWarnings("unchecked")
		List<ForwardCostom> forwardCostoms = pageInfo.getList();
		// 返回json数组分页
		JSONObject jsonObject = new JSONObject();
		JSONArray array = new JSONArray();
		for (Iterator<ForwardCostom> iterator = forwardCostoms.iterator(); iterator.hasNext();) {
			JSONObject jsonObject1 = new JSONObject();
			ForwardCostom forwardList = iterator.next();
			//YongHu yongHuCustom =yongHuService.findYongHuById(forwardList.getUserkey());
			//System.out.println("forwardList.getUserkey()----"+forwardList.getUserkey());
			SheBei shebei=sheBeiService.findSheBeiById(forwardList.getUserkey());
			jsonObject1.put("id",forwardList.getId() );
			jsonObject1.put("userkey", forwardList.getUserkey());
			if(shebei != null) {
				jsonObject1.put("name", shebei.getUserName());
			}
			jsonObject1.put("forwardUrl", forwardList.getForwardUrl());
			jsonObject1.put("createTime", MethodUtil.formatDate(3,forwardList.getCreateTime()));
			array.add(jsonObject1);
		}
		jsonObject.put("total", pageInfo.getTotal());
		jsonObject.put("rows", array);
		
		// 输出
		MethodUtil.writer(response, jsonObject.toString());
	}
	@RequestMapping("/forward/insert.html")
	public ModelAndView insert() throws Exception{
		//YongHuCustom yongHuCustom=new YongHuCustom();
		//List<YongHuCustom> yongHuCustoms=yongHuService.findYongHutList(yongHuCustom);
		List<SheBei> shebei=sheBeiService.findList();
		ModelAndView mv=new ModelAndView("/forward/insert");
		mv.addObject("shebei", shebei);
		return mv;
	}
	// 添加处理
	@ResponseBody
	@RequestMapping("/forward/insertDo.html")
	public Object insertDo(ForwardCostom forwardCostom,HttpServletRequest request) throws Exception {
		forwardCostom.setId(UuidUtil.get32UUID());
		forwardCostom.setCreateTime(MethodUtil.getDate(1, null));
		forwardService.insertForward(forwardCostom);
		return JsonResultUtil.getSuccessJson("成功！");
	}

/**

 *@描述 数据转发看单条，目前存在个别userkey拥有多个转发地址的问题

 *@参数

 *@返回值

 *@创建人  gyl

 *@创建时间  2018/9/5

 *@修改人和其它信息

 */
	@RequestMapping("/forward/update.html")
	public ModelAndView update(ForwardCostom forwardCostom) throws Exception{
		//YongHuCustom yongHuCustom=new YongHuCustom();
		//List<YongHuCustom> yongHuCustoms=yongHuService.findYongHutList(yongHuCustom);
		List<SheBei> shebei=sheBeiService.findList();
		ForwardCostom forwardCostoms=forwardService.findForwardById(forwardCostom.getUserkey());
		ModelAndView mv=new ModelAndView("/forward/update");
		mv.addObject("forwardCostoms", forwardCostoms);
		//mv.addObject("yongHuCustoms", yongHuCustoms);
		mv.addObject("shebei", shebei);
		return mv;
	}



	@ResponseBody
	@RequestMapping("/forward/updateDo.html")
	public Object updateDo(ForwardCostom forwardCostom) throws Exception{
		forwardService.updateForward(forwardCostom);
		return JsonResultUtil.getSuccessJson("成功！");
	}
	@ResponseBody
	@RequestMapping("/forward/del.html")
	public Object del(String id) throws Exception{
		forwardService.delForward(id);
		return JsonResultUtil.getSuccessJson("成功！");
	}
	
}
